﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Dungeon_Dive
{

    public enum Result { Fail, Win }

    /// <summary>
    /// Game Over screen displays game session's result.
    /// </summary>
    public class GameOverScreen : GameScreen
    {
        SpriteBatch spriteBatch;
        SpriteFont font;

        Log log;

        public Menu GameOverMenu { get; private set; }
        public String Text { get; set; }

        public Result EndState { get; set; }

        String victoryText;
        String deathText;
        Texture2D victoryPic;
        Texture2D deathPic;
        Texture2D pic;

        Rectangle deathPicBox;
        Rectangle victoryPicBox;
        Rectangle picBox;

        public GameOverScreen(Game1 game)
            : base(game)
        {
            GameOverMenu = new Menu(game);
            GameOverMenu.AddItem("Start over");
            GameOverMenu.AddItem("Quit");
            components.Add(GameOverMenu);

            log = (Log)game.Services.GetService(typeof(Log));               
        }

        protected override void LoadContent()
        {
            ContentManager contentManager = (ContentManager)parentGame.Services.GetService(typeof(ContentManager));
            spriteBatch = contentManager.SpriteBatch;
            font = parentGame.Content.Load<SpriteFont>("Fonts\\Debugfont");
            victoryText = "Congratulations!\nYou have succesfully navigated the dangerous" +
                        " levels of Dungeon Dive!";
            deathText = "You have died.";
            victoryPic = parentGame.Content.Load<Texture2D>("Textures//victory");
            deathPic = parentGame.Content.Load<Texture2D>("Textures//death");

            float scale = (Game.Window.ClientBounds.Width / 2 - 20) / (float)victoryPic.Width;
            deathPicBox = new Rectangle(10,
                            Game.Window.ClientBounds.Height/2 - deathPic.Height/2,
                            Game.Window.ClientBounds.Width/2 - 20,
                            (int)(deathPic.Height * scale));            
            victoryPicBox = new Rectangle(10,
                                (int)(Game.Window.ClientBounds.Height - victoryPic.Height * scale),
                                Game.Window.ClientBounds.Width / 2 - 20,
                                (int)(victoryPic.Height * scale));            
            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (EndState == Result.Win)
            {
                Text = victoryText;
                pic = victoryPic;
                picBox = victoryPicBox;
            }
            else
            {
                Text = deathText;
                pic = deathPic;
                picBox = deathPicBox;
            }

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront,
    SaveStateMode.SaveState);
            spriteBatch.DrawString(font, Text, new Vector2(100, 100), Color.White);
            spriteBatch.Draw(pic, picBox, Color.Gray);
            spriteBatch.End();

            GameOverMenu.Draw(gameTime);
            base.Draw(gameTime);
        }
    }
}
